﻿Public Class Form1

    Private m_pGalaxy As clsGalaxy
    Private m_pBackBuffer As Bitmap

    Private m_pPanOffset As New PointF(0.0F, 0.0F)
    Private m_bPanning As Boolean = False
    Private m_pLastKnownMousePosition As New PointF(0.0F, 0.0F)
    Private m_dZoomScale As Single = 1.0F


    Private Sub pTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pTimerRender.Tick
        Render()
    End Sub

    Private Sub Render()
        Dim bb As Graphics = Graphics.FromImage(m_pBackBuffer)
        bb.Clear(Color.Black)

        bb.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

        m_pGalaxy.Move(0.3)
        Dim pCenterStarPos As PointF = m_pGalaxy.GetCenterStar.GetCurrentPosition
        Dim pPlanetPos As PointF = Nothing

        bb.DrawRectangle(Pens.White, (pCenterStarPos.X * m_dZoomScale) + m_pPanOffset.X, (pCenterStarPos.Y * m_dZoomScale) + m_pPanOffset.Y, 0.8F, 0.8F)
        For Each pPlanet In m_pGalaxy.GetPlanets
            pPlanetPos = pPlanet.GetCurrentPosition
            bb.DrawRectangle(Pens.White, (pPlanetPos.X * m_dZoomScale) + m_pPanOffset.X, (pPlanetPos.Y * m_dZoomScale) + m_pPanOffset.Y, 0.5F, 0.5F)
        Next



        Dim g As Graphics = Me.CreateGraphics
        g.DrawImageUnscaled(m_pBackBuffer, 0, 0)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ClientSize = New Size(600, 800)
        Dim pScreenSize As Rectangle = My.Computer.Screen.WorkingArea

        Dim iX As Integer = (pScreenSize.Width - Me.ClientSize.Width) / 2
        Dim iY As Integer = (pScreenSize.Height - Me.ClientSize.Height) / 2

        Me.Location = New Point(iX, iY)

        m_pGalaxy = New clsGalaxy(New PointF(Me.ClientSize.Width / 2.0, Me.ClientSize.Height / 2.0), 5000)

        m_pBackBuffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)

        pTimerRender.Interval = 10
        pTimerRender.Enabled = True
        pTimerMove.Interval = 100
        pTimerMove.Enabled = False
    End Sub


    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        m_bPanning = True
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If m_bPanning Then
            m_pPanOffset.X += (e.Location.X - m_pLastKnownMousePosition.X)
            m_pPanOffset.Y += (e.Location.Y - m_pLastKnownMousePosition.Y)
        End If
        m_pLastKnownMousePosition = e.Location
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        m_bPanning = False
    End Sub

    Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        Dim iTicks As Integer = e.Delta \ 120
        Dim dZoomDif As Single = iTicks * 0.1
        m_dZoomScale += dZoomDif

        m_pPanOffset.X -= dZoomDif * m_pLastKnownMousePosition.X
        m_pPanOffset.Y -= dZoomDif * m_pLastKnownMousePosition.Y
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        m_pBackBuffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
    End Sub
End Class
